﻿--[[
    @brief    快速登录处理
    @Author    jameszhai
    @date    2017-05-16
--]]

EQKLoginUI = 
{
    sQuickLogin = "pnlQickLogin";
    sPnlAccBack = "pnlAccBG";
    sCbbAccList = "pnlAccBG\\pnlAccList";
    sBtnOthAcc  = "btnOtherAcc";
    sAccListUI  = "pnlAccBG\\pnlAccList\\gvAccList";
    sBtnPreAcc  = "pnlAccBG\\pnlAccList\\BtnPre";
    sBtnNextAcc = "pnlAccBG\\pnlAccList\\BtnNext";
    sBtnSgAcc   = "pnlAccBG\\pnlSingleAccount"

};

EQKLoginRes = 
{
    -- ITEM的背景
    sTopItemBG = "3HIMAGES:client_login\\login.png?810,10,830,40;"..
                 "client_login\\login.png?831,10,963,40;" ..
                 "client_login\\login.png?963,10,993,40;Stretch;0";
    sMidItemBG = "COLOR:0x2BD1FF";
    sOneItemBG = "3HIMAGES:client_login\\login.png?93,205,113,235;"..
                 "client_login\\login.png?119,205,129,235;"..
                 "client_login\\login.png?135,205,155,235;Stretch;0";
    sBtmItemBG = "3HIMAGES:client_login\\login.png?810,80,830,115;"..
                 "client_login\\login.png?831,80,963,115;"..
                 "client_login\\login.png?963,80,993,115;Stretch;0";
};

TQuickLogin = 
{
    -- 初始化
    InitView = function(dwRoot)
        TQuickLogin.dwViewID = dwRoot;
        ierd_tgp.tpf_ui.Require("wegame_login\\qrbutton.lua");
    end;

    -- 卸载
    UnInitView = function()
        TQRButton.UnInitView();

        TQuickLogin.pAccList = nil;
        TQuickLogin.pAccBack = nil;
        TQuickLogin.zAccList = nil;
        TQuickLogin.zItemMap = {};

        TQuickLogin.pLoginUI = nil;
        TQuickLogin.dwViewID = 0;
    end;

    ShowQRButton = function(bShow, bAni)
        TQRButton.ShowQRButton(bShow, bAni);
    end;

    -- 创建界面
    CheckUI = function(dwRoot)
        if nil ~= TQuickLogin.pLoginUI then
            return true;
        end;

        local dwRoot = ui_utility.get_ctrl_id_by_path(TQuickLogin.dwViewID, 
                                                EQKLoginUI.sQuickLogin);

        TQuickLogin.pLoginUI = ui_utility.get_ctrl_ptr_by_id(dwRoot);

        if nil == TQuickLogin.pLoginUI then
            TGPLog.LogE("QKLogin", "Get login pannel fail.");
            return false;
        end;

        -- 二维码登录
        if not TQRButton.InitView(dwRoot) then
            TGPLog.LogE("QKLogin", "QRButton init fail.");
            return false;
        end;

        -- 切换帐号
        local dwTemp = ui_utility.get_ctrl_id_by_path(dwRoot, EQKLoginUI.sBtnOthAcc);

        if nil ~= dwTemp and 0 ~= dwTemp then
            ui_utility.setproperty_by_id(dwTemp, "OnClick", "TQuickLogin.OnClkOther");
        else
            TGPLog.LogE("QKLogin", "Switch other account button is nil.");
        end;

        -- 账号列表背景
        TQuickLogin.pAccBack = ui_utility.get_ctrl_ptr_by_path(dwRoot, EQKLoginUI.sPnlAccBack);

        if (nil == TQuickLogin.pAccBack) then
            TGPLog.LogE("QKLogin", "Get account list back fail.");
            return false;
        end;

        -- 单账号按钮
        TQuickLogin.dwSingleAccBtnID = ui_utility.get_ctrl_id_by_path(dwRoot, EQKLoginUI.sBtnSgAcc);
        ui_utility.setproperty_by_id(TQuickLogin.dwSingleAccBtnID, "OnClick", "TQuickLogin.OnClkAcc");

        -- 帐号列表
        TQuickLogin.dwPnlAccList = ui_utility.get_ctrl_id_by_path(dwRoot, EQKLoginUI.sCbbAccList);
        TQuickLogin.dwPreBtnID = ui_utility.get_ctrl_id_by_path(dwRoot, EQKLoginUI.sBtnPreAcc);
        TQuickLogin.dwNextBtnID = ui_utility.get_ctrl_id_by_path(dwRoot, EQKLoginUI.sBtnNextAcc);
        TQuickLogin.pAccList = ui_utility.get_ctrl_ptr_by_path(dwRoot, EQKLoginUI.sAccListUI); 
        local dwAccView = ui_utility.get_ctrl_id_by_path(dwRoot, EQKLoginUI.sAccListUI);

        if (nil ~= TQuickLogin.pAccList) then
            -- 监听事件
            ui_utility.setproperty_by_id(dwAccView, "OnClickCell", 
                                      "TQuickLogin.OnClkAccCell");
            ui_utility.setproperty_by_id(dwAccView, "OnUpdateCell",
                                      "TQuickLogin.OnUpdateCell");

            TQuickLogin.UpdateAccList();
        end;
        return true;
    end;


    ShowView = function()
        if TLoginView ~= nil then
            TLoginView.ShowCapsTips(false);
        end;
        if TQuickLogin.CheckUI() then
            TQuickLogin.pLoginUI:Show(TRUE);
        else
            TGPLog.LogE("QKLogin", "Hide view, pannel is null.");
        end;
    end;

    HideView = function()
        if nil ~= TQuickLogin.pLoginUI then
            TQRButton.HideQRButton();
            TQuickLogin.pLoginUI:Show(FALSE);
        else
            TGPLog.LogE("QKLogin", "Hide view, pannel is null.");
        end;
    end;

    -- 设置帐号列表
    SetAccList = function(zAccList)
        TQuickLogin.zAccList = zAccList;
        TQuickLogin.UpdateAccList();
    end;

    -- 更新帐号列表
    UpdateAccList = function()
        if nil == TQuickLogin.pAccList then
            TGPLog.LogI("QKLogin", "Acc list not created.");
            return;
        end;

        if nil == TQuickLogin.zAccList then
            TGPLog.LogI("QKLogin", "Account list is null.");
            return;
        end;

        local     pUILib = ui_utility.uilib();
        if nil == pUILib then
            TGPLog.LogE("QKLogin", "Get UILib fail.");
            return;
        end;

        -- 切换重置当前选择
        TQuickLogin.curSelect = 0;

        local     nCount = TQuickLogin.zAccList.count;

        if 1 == nCount then
            ui_utility.setproperty_by_id(TQuickLogin.dwSingleAccBtnID, "Visible", "true");
            ui_utility.setproperty_by_id(TQuickLogin.dwPnlAccList, "Visible", "false");
            TQuickLogin.SetSingleAccBtnData();
        elseif nCount > 1 then
            ui_utility.setproperty_by_id(TQuickLogin.dwSingleAccBtnID, "Visible", "false");
            ui_utility.setproperty_by_id(TQuickLogin.dwPnlAccList, "Visible", "true");

            -- 标记开始更新数据
            TQuickLogin.pAccList:BeginUpdate();

            -- 只显示一行
            TQuickLogin.pAccList:SetRowCount(1);
            -- 已经加载成功本地列表
            TQuickLogin.pAccList:SetItemCount(0);   -- 先清空

            TQuickLogin.nHovItem = 0;
            TQuickLogin.pAccList:SetItemCount(nCount);

            if (nCount <= 0) then
                TGPLog.LogI("QKLogin", "Account list is empty.");
                return;
            end;


            -- 默认选中第一个
            TQuickLogin.pAccList:SetSelect(0);

            -- OK，刷新完成，更新布局
            TQuickLogin.pAccList:EndUpdate();

            -- 焦点切换到账号列表上
            TQuickLogin.pAccList:SetFocus();

            if TQuickLogin.curSelect + 2 >= TQuickLogin.zAccList.count then
                -- 右边没有节点了
                TQuickLogin.SetNextBtnEnable(false);
            end
            if 0 == TQuickLogin.curSelect then
                -- 左边没有节点了
                TQuickLogin.SetPreBtnEnable(false);
            end
        end
    end;

    -- 验证文件是否有效
    ValidFile = function(sFile)
        if nil == sFile then
            return false;
        end;
        local nSize = ierd_tgp.common.LuaGetFileSize(sFile);
        if (nSize > 0) then
            return true;
        end;
        return false;
    end;

    -- 点击帐号按钮
    OnClkAcc = function(pUILib, nCtrlID)
        local     nUserID = TQuickLogin.zItemMap[nCtrlID];

        if nil ~= nUserID then
            local     zParam = {};

            zParam["uin" ] = nUserID;
            zParam["type"] = ELoginType.ltQKLogin;

            comm_center.call_service("Svr_WGLogin", zParam);
        else
            TGPLog.LogE("QKLogin", "On click item, user id is nil.");
        end;
    end;

    -- 使用其它帐号登录
    OnClkOther = function(pUILib, nCtrlID)    
        local     zParam = {};

        zParam["nPageIdx"] = ELoginPage.lpAccLogin;
        comm_center.call_service("Svr_SwitchLoginPage", zParam);
        --comm_center.call_service("SvrShowFacebook", {});
    end;

    SetStatus = function(zParam)
    end;

    --test
    OnClkQRBtn = function(pUILib, nBtnID)
        local     zParam = {};
        zParam["nPageIdx"] = ELoginPage.lpWeChatQRLogin;
        comm_center.call_service("Svr_SwitchLoginPage", zParam);
    end;

    OnClkAccCell = function (pUILib, dwCtrlID, nIndex)
        local     nUserID = TQuickLogin.zItemMap[dwCtrlID];

        if nil ~= nUserID then
            local     zParam = {};

            zParam["uin" ] = nUserID;
            zParam["type"] = ELoginType.ltQKLogin;

            comm_center.call_service("Svr_WGLogin", zParam);
        else
            TGPLog.LogE("QKLogin", "On click item, user id is nil.");
        end;
    end;

    OnUpdateCell = function (pUILib, dwCtrlID, nIndex)
        TQuickLogin.UpdataAccData(dwCtrlID, nIndex, 5);
    end;

    UpdataAccData = function (dwCtrlID, nIndex, nMaxChar)
       if 0 ~= dwCtrlID  and nil ~= TQuickLogin.zAccList then
            -- 昵称
            local lblNick = ui_utility.get_ctrl_ptr_by_path(dwCtrlID, "lblNick");
            if nil ~= lblNick then
                local   sTemp = TQuickLogin.zAccList.list[nIndex].name;

                if ("" == sTemp) then
                    sTemp = tostring(TQuickLogin.zAccList.list[nIndex].uin);
                end;

                -- 建立映射
                TQuickLogin.zItemMap[dwCtrlID] = TQuickLogin.zAccList.list[nIndex].uin;

                local   nSize = #sTemp;

                if (nSize > nMaxChar) then
                    local   nIdx = 1;
                    local   nText = 0;

                    while (nIdx <= nSize) do
                        local   cTemp = string.byte(sTemp, nIdx);
                        local   cByte = 1;

                        if cTemp > 0 and cTemp <= 127 then      -- 普通字符
                            cByte = 1;
                            nText = nText + 1;
                        else                                    -- 汉字
                            nText = nText + 2;

                            if cTemp >= 192 and cTemp < 223 then
                                cByte = 2;                            
                            elseif cTemp >= 224 and cTemp < 239 then
                                cByte = 3;
                            else    -- if cTemp >= 240 and cTemp <= 247 then
                                cByte = 4;
                            end;
                        end;

                        nIdx = nIdx + cByte;

                        -- 超过4个字符就截断
                        if (nText / 2) >= nMaxChar then
                            if nIdx < nSize then
                                sTemp = string.sub(sTemp, 1, nIdx - 1) .. "...";
                            end;

                            break;
                        end;
                    end;
                end;

                local   sNick = U8toCUIStr(sTemp);
                lblNick:SetText(sNick);
            end;

            -- 头像
            local dwIcon = ui_utility.get_ctrl_id_by_path(dwCtrlID, "imgCtn\\imgIcon");
            if nil ~= dwIcon then
                local  sIcon = TQuickLogin.zAccList.list[nIndex].head;
                local  defaultIcon = "client_login\\default_head.png";
                if (nil ~= string.find(sIcon, "http:") or nil ~= string.find(sIcon, "https:")) then
                    sIcon = "ONEIMAGE:" .. defaultIcon .. "|" .. sIcon;
                elseif ("" == sIcon) then
                    sIcon = "ONEIMAGE:" .. defaultIcon;
                else
                    sIcon = "ONEIMAGE:" .. sIcon;
                end;

                sIcon = sIcon .. ";Circle;0;";
                ui_utility.setproperty_by_id(dwIcon, "Background", sIcon);
            else
                TGPLog.LogE("TQuickLogin", "Update cell, icon is nil.");
            end;
        else
            TGPLog.LogI("TQuickLogin", "No need to update cell:" .. tostring(nIndex));
        end;
    end;

    OnClickNextBtn = function (pUILib, nCtrlID)
        if nil == TQuickLogin.pAccList then
            return;
        end
        TQuickLogin.pAccList:SetSelect(TQuickLogin.curSelect + 2);
        TQuickLogin.curSelect = TQuickLogin.curSelect + 1;
        if TQuickLogin.curSelect + 2 >= TQuickLogin.zAccList.count then
            -- 右边没有节点了
            TQuickLogin.SetNextBtnEnable(false);
        end
        -- 设置pre按钮可以点击
        TQuickLogin.SetPreBtnEnable(true);
    end;

    OnClickPreBtn = function (pUILib, nCtrlID)
        if nil == TQuickLogin.pAccList then
            return;
        end
        TQuickLogin.pAccList:SetSelect(TQuickLogin.curSelect - 1);
        TQuickLogin.curSelect = TQuickLogin.curSelect - 1;
        if 0 == TQuickLogin.curSelect then
            -- 左边没有节点了
            TQuickLogin.SetPreBtnEnable(false);
        end
        -- 设置next按钮可以点击
        TQuickLogin.SetNextBtnEnable(true);
    end;

    SetSingleAccBtnData = function ()
        TQuickLogin.UpdataAccData(TQuickLogin.dwSingleAccBtnID, 0, 6);
    end;

    SetPreBtnEnable = function (bEnable)
        if bEnable then
            ui_utility.setproperty_by_id(TQuickLogin.dwPreBtnID, "Disable", "false");
            ui_utility.setproperty_by_id(TQuickLogin.dwPreBtnID, "Background",
                "ONEIMAGE:client_login\\login_input.png?455,490,462,501;Center;0;");
        else
            ui_utility.setproperty_by_id(TQuickLogin.dwPreBtnID, "Disable", "true");
            ui_utility.setproperty_by_id(TQuickLogin.dwPreBtnID, "Background",
                "ONEIMAGE:client_login\\login_input.png?525,490,532,501;Center;0;");
        end
    end;

    SetNextBtnEnable = function (bEnable)
        if bEnable then
            ui_utility.setproperty_by_id(TQuickLogin.dwNextBtnID, "Disable", "false");
            ui_utility.setproperty_by_id(TQuickLogin.dwNextBtnID, "Background",
                "ONEIMAGE:client_login\\login_input.png?456,459,463,470;Center;0;");
        else
            ui_utility.setproperty_by_id(TQuickLogin.dwNextBtnID, "Disable", "true");
            ui_utility.setproperty_by_id(TQuickLogin.dwNextBtnID, "Background",
                "ONEIMAGE:client_login\\login_input.png?526,459,533,470;Center;0;");
        end
    end;


    dwViewID = 0;
    pLoginUI = nil;        -- Login pannel
    pAccList = nil;        -- 帐号列表
    pAccBack = nil;
    dwPnlAccList = 0;
    dwPreBtnID = 0;
    dwNextBtnID = 0;
    dwSingleAccBtnID = 0;  -- 单账号按钮

    nHovItem = 0;
    curSelect = 0;

    -- 历史帐号
    zAccList = nil;

    -- CtrlID <==> UIN
    zItemMap = {};

};